<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程编号" prop="flowNo">
        <el-input
          v-model="queryParams.flowNo"
          placeholder="请输入流程编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批名称" prop="flowName">
        <el-input
          v-model="queryParams.flowName"
          placeholder="请输入审批名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批分组" prop="flowGroup">
        <el-input
          v-model="queryParams.flowGroup"
          placeholder="请输入审批分组"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批图标" prop="flowImg">
        <el-input
          v-model="queryParams.flowImg"
          placeholder="请输入审批图标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批人去重 1" prop="autoRepeat">
        <el-input
          v-model="queryParams.autoRepeat"
          placeholder="请输入审批人去重 1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发起人审批时自动通过 1" prop="myAuditAutoPass">
        <el-input
          v-model="queryParams.myAuditAutoPass"
          placeholder="请输入发起人审批时自动通过 1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批意见必填 1" prop="remarkRequired">
        <el-input
          v-model="queryParams.remarkRequired"
          placeholder="请输入审批意见必填 1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批意见对发起人不见 1" prop="notVisibleForSponsor">
        <el-input
          v-model="queryParams.notVisibleForSponsor"
          placeholder="请输入审批意见对发起人不见 1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修订" prop="REVISE">
        <el-input
          v-model="queryParams.REVISE"
          placeholder="请输入修订"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否撤回" prop="WITHDRAW">
        <el-input
          v-model="queryParams.WITHDRAW"
          placeholder="请输入是否撤回"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否多岗位支持" prop="multiPositionEnable">
        <el-input
          v-model="queryParams.multiPositionEnable"
          placeholder="请输入是否多岗位支持"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除标志 (0代表删除 1代表存在）" prop="delFlag">
        <el-input
          v-model="queryParams.delFlag"
          placeholder="请输入删除标志 (0代表删除 1代表存在）"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入更新人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable
          v-model="queryParams.updateTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['flow:definition:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['flow:definition:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flow:definition:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['flow:definition:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  v-loading="loading" :data="definitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="ID" />
      <el-table-column label="流程编号" align="center" prop="flowNo" />
      <el-table-column label="审批类型" align="center" prop="flowType" />
      <el-table-column label="审批名称" align="center" prop="flowName" />
      <el-table-column label="审批分组" align="center" prop="flowGroup" />
      <el-table-column label="审批图标" align="center" prop="flowImg" />
      <el-table-column label="审批说明" align="center" prop="flowRemark" />
      <el-table-column label="审批人去重 1" align="center" prop="autoRepeat" />
      <el-table-column label="发起人审批时自动通过 1" align="center" prop="myAuditAutoPass" />
      <el-table-column label="审批意见必填 1" align="center" prop="remarkRequired" />
      <el-table-column label="审批意见对发起人不见 1" align="center" prop="notVisibleForSponsor" />
      <el-table-column label="审批意见填写提示" align="center" prop="remarkTip" />
      <el-table-column label="审批JSON" align="center" prop="JSON" />
      <el-table-column label="修订" align="center" prop="REVISE" />
      <el-table-column label="是否撤回" align="center" prop="WITHDRAW" />
      <el-table-column label="是否多岗位支持" align="center" prop="multiPositionEnable" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="状态 0正常 1停用" align="center" prop="STATUS" />
      <el-table-column label="删除标志 (0代表删除 1代表存在）" align="center" prop="delFlag" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['flow:definition:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['flow:definition:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程定义对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="definitionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程编号" prop="flowNo">
          <el-input v-model="form.flowNo" placeholder="请输入流程编号" />
        </el-form-item>
        <el-form-item label="审批名称" prop="flowName">
          <el-input v-model="form.flowName" placeholder="请输入审批名称" />
        </el-form-item>
        <el-form-item label="审批分组" prop="flowGroup">
          <el-input v-model="form.flowGroup" placeholder="请输入审批分组" />
        </el-form-item>
        <el-form-item label="审批图标" prop="flowImg">
          <el-input v-model="form.flowImg" placeholder="请输入审批图标" />
        </el-form-item>
        <el-form-item label="审批说明" prop="flowRemark">
          <el-input v-model="form.flowRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审批人去重 1" prop="autoRepeat">
          <el-input v-model="form.autoRepeat" placeholder="请输入审批人去重 1" />
        </el-form-item>
        <el-form-item label="发起人审批时自动通过 1" prop="myAuditAutoPass">
          <el-input v-model="form.myAuditAutoPass" placeholder="请输入发起人审批时自动通过 1" />
        </el-form-item>
        <el-form-item label="审批意见必填 1" prop="remarkRequired">
          <el-input v-model="form.remarkRequired" placeholder="请输入审批意见必填 1" />
        </el-form-item>
        <el-form-item label="审批意见对发起人不见 1" prop="notVisibleForSponsor">
          <el-input v-model="form.notVisibleForSponsor" placeholder="请输入审批意见对发起人不见 1" />
        </el-form-item>
        <el-form-item label="审批意见填写提示" prop="remarkTip">
          <el-input v-model="form.remarkTip" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审批JSON" prop="JSON">
          <el-input v-model="form.JSON" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="修订" prop="REVISE">
          <el-input v-model="form.REVISE" placeholder="请输入修订" />
        </el-form-item>
        <el-form-item label="是否撤回" prop="WITHDRAW">
          <el-input v-model="form.WITHDRAW" placeholder="请输入是否撤回" />
        </el-form-item>
        <el-form-item label="是否多岗位支持" prop="multiPositionEnable">
          <el-input v-model="form.multiPositionEnable" placeholder="请输入是否多岗位支持" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="删除标志 (0代表删除 1代表存在）" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志 (0代表删除 1代表存在）" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Definition">
import { listDefinition, getDefinition, delDefinition, addDefinition, updateDefinition } from "@/api/flow/definition";

const { proxy } = getCurrentInstance();

const definitionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    flowNo: null,
    flowType: null,
    flowName: null,
    flowGroup: null,
    flowImg: null,
    flowRemark: null,
    autoRepeat: null,
    myAuditAutoPass: null,
    remarkRequired: null,
    notVisibleForSponsor: null,
    remarkTip: null,
    JSON: null,
    REVISE: null,
    WITHDRAW: null,
    multiPositionEnable: null,
    orderNum: null,
    STATUS: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询流程定义列表 */
function getList() {
  loading.value = true;
  listDefinition(queryParams.value).then(response => {
    definitionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    ID: null,
    flowNo: null,
    flowType: null,
    flowName: null,
    flowGroup: null,
    flowImg: null,
    flowRemark: null,
    autoRepeat: null,
    myAuditAutoPass: null,
    remarkRequired: null,
    notVisibleForSponsor: null,
    remarkTip: null,
    JSON: null,
    REVISE: null,
    WITHDRAW: null,
    multiPositionEnable: null,
    orderNum: null,
    STATUS: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("definitionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.ID);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加流程定义";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _ID = row.ID || ids.value
  getDefinition(_ID).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改流程定义";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["definitionRef"].validate(valid => {
    if (valid) {
      if (form.value.ID != null) {
        updateDefinition(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDefinition(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _IDs = row.ID || ids.value;
  proxy.$modal.confirm('是否确认删除流程定义编号为"' + _IDs + '"的数据项？').then(function() {
    return delDefinition(_IDs);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('flow/definition/export', {
    ...queryParams.value
  }, `definition_${new Date().getTime()}.xlsx`)
}

getList();
</script>
